/**
 * 
 */
package org.easyframework.core.utils;

import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;

import org.easyframework.core.entity.ProcessResult;
import org.hibernate.validator.HibernateValidator;

/**
 * @ClassName: ValidationUtils
 * @Description: 校验工具类
 * @author: daiw
 * @date: 2019年10月17日 下午3:59:41
 */
public class ValidationUtils {
	/**
	 * 使用hibernate的注解来进行验证
	 * 
	 */
	private static Validator validator = Validation.byProvider(HibernateValidator.class).configure().failFast(true).buildValidatorFactory().getValidator();

	/**
	 * 功能描述: <br>
	 * 〈注解验证参数〉
	 *
	 * @param obj
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	public static <T> ProcessResult<String> validate(T obj) {
		if (obj == null) {
			return ProcessResult.getInstFailed("找不到对象!");
		}
		Set<ConstraintViolation<T>> constraintViolations = validator.validate(obj);
		// 抛出检验异常
		if (constraintViolations.size() > 0) {
			return ProcessResult.getInstFailed(constraintViolations.iterator().next().getMessage());
		}
		return ProcessResult.getInstSuccess();
	}
}
